//
//  Tool.m
//  MasonryDemo
//
//  Created by 王鑫 on 16/3/4.
//  Copyright © 2016年 王鑫. All rights reserved.
//

#import "Tool.h"

@implementation Tool

+ (NSString*) getBoxPath{
    NSString * boxPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",boxPath);
    return boxPath;
}
+ (NSString*) getCachesPath{
    NSString * cachesPath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSLog(@"%@",cachesPath);
    return cachesPath;
}
+ (CGFloat )getCachesSize{
    NSString * cachesPath=[self getCachesPath];
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:cachesPath]){
        return 0;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:cachesPath] objectEnumerator];
    NSString* fileName;
    CGFloat folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString* fileAbsolutePath = [cachesPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize;
}
+ (float) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize]/(1024.0*1024);
    }
    return 0;
}
+(void)removeCaches{
    
    NSString * cachesPath=[self getCachesPath];
    NSFileManager *manager=[NSFileManager defaultManager];
    NSArray *fileList = [[NSArray alloc] init];
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    fileList = [manager contentsOfDirectoryAtPath:cachesPath error:nil];
    //NSLog(@"%@",fileList);
    for (NSString *str in fileList) {
        NSError *error;
        NSString * path=  [cachesPath stringByAppendingPathComponent:str];
         [manager removeItemAtPath:path error:&error];
        //NSLog(@"=====%d",result);
        //NSLog(@"=====%@",error);
    }
}

/**---------------------------------------------AFNetworking--------------------------------------*/

+(void)GET:(NSString*)url black:(data)black{
        AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
         // manager.requestSerializer.cachePolicy =NSURLRequestReturnCacheDataElseLoad;
    [manager GET:url parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         black(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误接口:%@",url);
        NSLog(@"错误信息%@",error);
    }];
}
+(void)POST:(NSString*)url parameters:(NSDictionary *)dic black:(data)black{
     AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    [manager POST:url parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
           black(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"错误接口:%@",url);
        NSLog(@"错误信息%@",error);
    }];
}
@end
